package com.py.credit.service.impl;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.py.credit.dao.CreditReportDao;
import com.py.credit.dao.CreditReportLogDao;
import com.py.credit.dao.WebCreditReportDao;
import com.py.credit.entity.Hcbg;
import com.py.credit.entity.TableResult;
import com.py.credit.service.SSearchService;
import com.py.credit.service.ScontentService;
import com.py.credit.service.WebCreditReportService;
import com.py.credit.utils.DateUtils;
import com.py.framework.core.common.Page;
@Service
public class WebCreditReportServiceImpl implements WebCreditReportService {
	@Autowired
	private WebCreditReportDao webcreditreportDao;
	@Autowired
	private CreditReportDao creditReportDao;
	@Autowired
	private CreditReportLogDao creditReportLogDao;
	
	@Autowired
	private ScontentService sContentService;

	@Autowired
	private SSearchService sSearchService;

	@Autowired
	private BgdzServiceImpl bgdzService;
	@Override
	public  Map<String,String>   checkFr(String ztmc, String A00002, String gszch) {
		
		return webcreditreportDao.checkFr(ztmc,A00002,gszch);
	}

	@Override
	public  Map<String,String>   checkZrr(String fddbrmc, String fddbrzjhm) {
		
		return webcreditreportDao.checkZrr(fddbrmc,fddbrzjhm);
	}

	@Override
	public void addcreditreport(String ztmc, String tydm, String gszch, String fddbrmc, String fddbrzjhm, String jbrxm,
			String jbrzjhm, String jbrlxdh, String cxyt, String type,String path,String sqbh) {
		webcreditreportDao.addcreditreport(ztmc,tydm,gszch,fddbrmc,fddbrzjhm,jbrxm,jbrzjhm,jbrlxdh,cxyt,type,path,sqbh);
		
	}
	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> getReportData(String tybm, String kId) {
		String number = this.getReportSerials(kId, DateUtils.getCurrentTime());

		Map<String, Object> result = new HashMap<String, Object>();

		Map<String, Object> detail = sContentService.getDetail(kId, tybm, "ZWCX", null);

		Map<String, List<TableResult>> map = new HashMap<String, List<TableResult>>();

		List<TableResult> assembleTableComponent = sSearchService
				.assembleTableComponent((List<TableResult>) detail.get("results"));

		for (TableResult tableResult : assembleTableComponent) {

			String categoryCode = tableResult.getCategoryCode();

			if (map.containsKey(categoryCode)) {

				List<TableResult> list = map.get(categoryCode);

				list.add(tableResult);

			} else {

				List<TableResult> rs = new ArrayList<TableResult>();

				rs.add(tableResult);

				map.put(categoryCode, rs);

			}

		}

		Hcbg latest = bgdzService.getLatest();

		latest.setReportNo(latest.getReportNo() + DateUtils.getStringOfTodayDate(DateUtils.SIMPLE_PATTERN) + number);

		result.put("tableData", map);

		result.put("elementData", latest);

		return result;

	}

	@Override
	public synchronized String getReportSerials(String kId, String currentTime) {

		int serialNumbe = creditReportDao.selectReportSerials(kId, currentTime) + 1;

		creditReportDao.addReportSerials(serialNumbe, kId, currentTime);

		if (serialNumbe > 999) {

			serialNumbe = serialNumbe - 999;

		}

		return new DecimalFormat("000").format(serialNumbe).toString();

	}

	@Override
	public  List<Map<String,String>>  getcreditreportHistoryForPage(String ztmc, String lqzt, String type,Page page) {
		
		return webcreditreportDao.getcreditreportHistoryForPage(ztmc,lqzt,type,page);
	}

	@Override
	public void updatecreditreportStatus(String sqbh) {
		webcreditreportDao.updatecreditreportStatus(sqbh);
	}

	@Override
	public String creditreportPreview(String sqbh) {
		
		return webcreditreportDao.creditreportPreview(sqbh);
	}

	
}
